<?php
namespace app\api_wp_v01\controller;

class StaffRecycleOrder extends Staff {
	
	/**
	 * 统计 所有
	 */
	public function statistics_all() {
		
		$immature_num = \app\common\model\RecycleOrderModel::where('staff_id', $this->staff_id)
			->where('status', 2)
			->count('id');
		
		$time = time();
		$time_start = strtotime(date('Y-m-01', $time));
		$time_final = strtotime(date('Y-m-01', $time) . ' +1 month -1 second');
		$complete_num = \app\common\model\RecycleOrderModel::where('staff_id', $this->staff_id)
			->where('status', 3)
			->where('finish_time', 'between', array($time_start, $time_final))
			->count('id');
		
		return $this->disposeJson(0, '', array(
			'immature_num' => $immature_num,
			'complete_num' => $complete_num,
		));
	}
	
	/**
	 * 统计 已完成
	 */
	public function statistics_complete() {
		
		$field = array(
			'COUNT(`id`) AS `count_number`',
			'FROM_UNIXTIME(`accept_time`, "%Y-%m") AS `date_string`',
		);
		$date_select = \app\common\model\RecycleOrderModel::field($field)
			->where('status', 3)
			->where('staff_id', $this->staff_id)
			->group('date_string')
			->select();
		
		$date_list = array();
		foreach($date_select as $RecycleOrderModel){
			$date_list[] = array(
				'date_string' => $RecycleOrderModel->date_string,
				'count_number' => $RecycleOrderModel->count_number,
			);
		};
		
		return $this->disposeJson(0, '', array(
			'date_list' => $date_list
		));
	}
	
	/**
	 * 列表 未接受
	 */
	public function list_unaccept() {
		
		// 获取用户列表
		$region_select = \app\common\model\StaffRegionModel::where('staff_id', $this->staff_id)->select();
		if(0 === count($region_select)){
			return $this->disposeJson(0, '', array(
				'order_list' => array(),
			));
		};
		$region_list = array();
		foreach($region_select as $StaffRegionModel){
			$region_list[] = $StaffRegionModel->region_id;
		};
		
		// 校验传入参数
		$parameter = \think\Request::instance()->get();
		$judge_1 = array_key_exists('page', $parameter) && is_string($parameter['page']) && settype($parameter['page'], 'integer') && (0 < $parameter['page']);
		$judge_2 = array_key_exists('limit', $parameter) && is_string($parameter['limit']) && settype($parameter['limit'], 'integer') && (0 < $parameter['limit']);
		if(!$judge_1 || !$judge_2){
			return $this->disposeJson(1, '参数校验失败');
		};
		
		// 查询数据列表
		$field = array(
			'`recycle_order`.`id`', '`recycle_order`.`action_date`', '`recycle_order`.`action_time`', '`recycle_order`.`address`', '`recycle_order`.`address_describe`',
			'`region`.`name_full` AS `region_name`',
		);
		$order_select = \app\common\model\RecycleOrderModel::field($field)
			->join('region', '`region`.`id` = `recycle_order`.`region_district`', 'LEFT')
			->where('recycle_order.status', 1)
			->where('recycle_order.region_district', 'in', $region_list)
			->order('recycle_order.id DESC')
			->limit($parameter['limit'])->page($parameter['page'])
			->select();
		if(0 === count($order_select)){
			return $this->disposeJson(1, '没有更多的数据了');
		};
		
		$order_list = array();
		foreach($order_select as $RecycleOrderModel){
			$order_list[] = array(
				'id' => $RecycleOrderModel->id,
				'action_date' => date('Y-m-d', $RecycleOrderModel->action_date),
				'action_time' => $RecycleOrderModel->action_time,
				'address' => $RecycleOrderModel->address,
				'region_name' => $RecycleOrderModel->region_name,
			);
		};
		
		return $this->disposeJson(0, '', array(
			'order_list' => $order_list
		));
	}
	
	/**
	 * 列表 进行中
	 */
	public function list_progress() {
		
		$limit = 10;
		$parameter = \think\Request::instance()->get();
		$judge_1 = array_key_exists('page', $parameter) && is_string($parameter['page']) && settype($parameter['page'], 'integer') && (0 <= $parameter['page']);
		if(!$judge_1){
			return $this->disposeJson(1, '参数校验失败');
		};
		
		$field = array(
			'`recycle_order`.`id`', '`recycle_order`.`action_date`', '`recycle_order`.`action_time`', '`recycle_order`.`address`', '`recycle_order`.`address_describe`',
			'`recycle_order`.`position_lat`', '`recycle_order`.`position_lng`', 
			'`region`.`name_full` AS `region_name`',
		);
		$order_select = \app\common\model\RecycleOrderModel::field($field)
			->where('recycle_order.status', 2)
			->where('recycle_order.staff_id', $this->staff_id)
			->join('region', '`region`.`id` = `recycle_order`.`region_district`', 'LEFT')
			->order('recycle_order.id DESC');
			
		if(0 < $parameter['page']){
			$order_select ->limit($limit)->page($parameter['page']);
		};
		
		$order_list = array();
		$order_select = $order_select->select();
		foreach($order_select as $RecycleOrderModel){
			$order_list[] = $RecycleOrderModel->toArray();
		};
		
		$order_list = array();
		foreach($order_select as $RecycleOrderModel){
			$order_list[] = array(
				'id' => $RecycleOrderModel->id,
				'action_date' => date('Y-m-d', $RecycleOrderModel->action_date),
				'action_time' => $RecycleOrderModel->action_time,
				'address' => $RecycleOrderModel->address,
				'region_name' => $RecycleOrderModel->region_name,
				'position_lat' => $RecycleOrderModel->position_lat,
				'position_lng' => $RecycleOrderModel->position_lng,
			);
		};
		
		return $this->disposeJson(0, '', array(
			'order_list' => $order_list,
			'limit' => $limit,
		));
		
	}
	
	public function get_detail() {
		
		$parameter = \think\Request::instance()->post();
		$judge_1 = array_key_exists('id', $parameter) && is_integer($parameter['id']) && (0 < $parameter['id']);
		if(!$judge_1){
			return $this->disposeJson(1, '参数校验失败');
		};
		
		$RecycleOrderModel = \app\common\model\RecycleOrderModel::get($parameter['id']);
		if(null === $RecycleOrderModel){
			return $this->disposeJson(1, '目标不存在，请刷新后重试');
		};
		
		return $this->disposeJson(0, '', array(
			'genre' => $RecycleOrderModel->genre,
			'genre_name' => $RecycleOrderModel->getGenreName(),
			'action_date' => date('Y-m-d', $RecycleOrderModel->action_date),
			'action_time' => $RecycleOrderModel->action_time,
			'remark' => $RecycleOrderModel->remark,
			'liaison_name' => $RecycleOrderModel->liaison_name,
			'liaison_phone' => $RecycleOrderModel->liaison_phone,
			'position_lat' => $RecycleOrderModel->position_lat,
			'position_lng' => $RecycleOrderModel->position_lng,
			'address' => $RecycleOrderModel->address,
			'address_describe' => $RecycleOrderModel->address_describe,
			'create_time' => date('Y-m-d H:i:s', $RecycleOrderModel->create_time),
		));
	}
	
	public function action_accept() {
		
		$parameter = \think\Request::instance()->post();
		$judge_1 = array_key_exists('id', $parameter) && is_integer($parameter['id']) && (0 < $parameter['id']);
		if(!$judge_1){
			return $this->disposeJson(1, '参数校验失败');
		};
		
		if(0 !== $this->staff_info['is_disable']){
			return $this->disposeJson(1, '您的账户被禁用，暂时不能接单');
		};
		
		// 获取订单
		$RecycleOrderModel = \app\common\model\RecycleOrderModel::get($parameter['id']);
		if(null === $RecycleOrderModel){
			return $this->disposeJson(1, '目标不存在，请刷新后重试');
		}else if(1 !== $RecycleOrderModel->status){
			return $this->disposeJson(1, '该订单已经被接取');
		};
		
		
		// 获取员工地区
		$judge = false;
		$region_select = \app\common\model\StaffRegionModel::where('staff_id', $this->staff_id)->select();
		foreach($region_select as $StaffRegionModel){
			if($RecycleOrderModel->region_district === $StaffRegionModel->region_id){
				$judge = true;
				break;
			};
		};
		if(false === $judge){
			return $this->disposeJson(1, '该订单不属于您的范围以内');
		};
		
		$time = time();
		\app\common\model\Model::startTrans();
		try{
			
			// 保存订单状态
			$RecycleOrderModel->status = 2;
			$RecycleOrderModel->accept_time = $time;
			$result = $RecycleOrderModel->save();
			if(false == $result){
				throw new \Exception('保存订单状态失败');
			};
			
			// 绑定人员信息
			$result = \app\common\model\RecycleOrderModel::where('id', $RecycleOrderModel->id)->where('staff_id', 0)->update(['staff_id' => $this->staff_id]);
			if(1 !== $result){
				throw new \Exception('绑定人员信息失败');
			};
			
			\app\common\model\Model::commit();
			
		}catch(\Exception $error){
			
			\app\common\model\Model::rollback();
			$message = is_string($error->getMessage()) ? $error->getMessage() : json_encode($error->getMessage());
			return $this->disposeJson(1, $message);
			
		};
		
		return $this->disposeJson(0, '接取成功');
		
	}
	
	public function scan_progress() {
		
	}
	
	public function disose_complete() {
		
		$parameter = \think\Request::instance()->post();
		$judge_1 = array_key_exists('id', $parameter) && is_integer($parameter['id']) && (0 < $parameter['id']);
		$judge_2 = array_key_exists('number', $parameter) && (is_integer($parameter['number']) || is_float($parameter['number'])) && (0 <= $parameter['number']);
		if(!$judge_1 || !$judge_2){
			return $this->disposeJson(1, '参数校验失败');
		};
		
		// 获取订单信息
		$RecycleOrderModel = \app\common\model\RecycleOrderModel::get($parameter['id']);
		if(null === $RecycleOrderModel){
			return $this->disposeJson(1, '目标不存在，请刷新后重试');
		}else if($this->staff_id !== $RecycleOrderModel->staff_id){
			return $this->disposeJson(1, '该订单不属于你');
		};
		
		// 获取用户信息
		$SubscriberModel = \app\common\model\SubscriberModel::get($RecycleOrderModel->subscriber_id);
		if(null === $SubscriberModel){
			return $this->disposeJson(1, '该订单对应的用户不存在');
		};
		
		$time = time();
		\app\common\model\Model::startTrans();
		try{
			
			// 修改订单时间
			$RecycleOrderModel->finish_time = $time;
			$result = $RecycleOrderModel->save();
			if(false == $result){
				throw new \Exception('修改订单时间失败');
			};
			
			// 修改订单状态
			$result = \app\common\model\RecycleOrderModel::where('id', $RecycleOrderModel->id)->where('status', 2)->update(['status' => 3]);
			if(1  !== $result){
				throw new \Exception('修改订单状态失败');
			};
			
			// 写入订单完结数量
			$RecycleOrderNumberModel = new \app\common\model\RecycleOrderNumberModel();
			$RecycleOrderNumberModel->order_id = $RecycleOrderModel->id;
			$RecycleOrderNumberModel->group_id = $SubscriberModel->group_id;
			$RecycleOrderNumberModel->staff_id = $this->staff_id;
			$RecycleOrderNumberModel->number = $parameter['number'];
			$RecycleOrderNumberModel->create_time = $time;
			$result = $RecycleOrderNumberModel->save();
			if(false == $result){
				throw new \Exception('写入订单完结数量失败');
			};
			
			\app\common\model\Model::commit();
			
		}catch(\Exception $error){
			
			\app\common\model\Model::rollback();
			$message = is_string($error->getMessage()) ? $error->getMessage() : json_encode($error->getMessage());
			return $this->disposeJson(1, $message);
			
		};
		
		return $this->disposeJson(0, '操作成功');
		
	}
	
	
}